package com.smile.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smile.api.AirportApi;
import com.smile.api.OrganizationApi;
import com.smile.mapper.InspectionTaskMapper;
import com.smile.model.domain.FeisuanInspectionTask;
import com.smile.model.dto.request.GetInspectionTaskByFlightIdRequestDTO;
import com.smile.model.response.response.GetInspectionTaskByFlightIdResponseDTO;
import com.smile.service.InspectionTaskService;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class InspectionTaskServiceImpl implements InspectionTaskService {

    @Autowired
    private InspectionTaskMapper inspectionTaskMapper;

    @Autowired
    private AirportApi airportApi;

    @Autowired
    private OrganizationApi organizationApi;



    @Override
    public GetInspectionTaskByFlightIdResponseDTO getInspectionTaskByFlightId(GetInspectionTaskByFlightIdRequestDTO getInspectionTaskByFlightIdRequestDTO) {
        String flightId = getInspectionTaskByFlightIdRequestDTO.getFlightId();
        QueryWrapper<FeisuanInspectionTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flight_id", flightId);
        FeisuanInspectionTask feisuanInspectionTasks = inspectionTaskMapper.selectOne(queryWrapper);
        GetInspectionTaskByFlightIdResponseDTO getInspectionTaskByFlightIdResponseDTO = new GetInspectionTaskByFlightIdResponseDTO();
        getInspectionTaskByFlightIdResponseDTO.setFlightId(flightId);
        getInspectionTaskByFlightIdResponseDTO.setAirportId(feisuanInspectionTasks.getAirlineId());
        return getInspectionTaskByFlightIdResponseDTO;
    }

//    @Override
//    public FeisuanInspectionTask getInspectionTaskByBid(String bid) {
//
//        QueryWrapper<FeisuanInspectionTask> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("bid", bid);
//        queryWrapper.last("limit 1");
//        FeisuanInspectionTask feisuanInspectionTasks = inspectionTaskMapper.selectOne(queryWrapper);
//        return feisuanInspectionTasks;
//    }

    @Override
    public void editInspectionTaskByFlightId(String flightId,Integer errorCode,Integer status,Integer currentWaypointIndex){
        QueryWrapper<FeisuanInspectionTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flight_id", flightId);
        FeisuanInspectionTask feisuanInspectionTasks = inspectionTaskMapper.selectOne(queryWrapper);
        if(feisuanInspectionTasks!=null){
            feisuanInspectionTasks.setErrorCode(errorCode);
            feisuanInspectionTasks.setStatus(status);
            if (currentWaypointIndex != null){
                feisuanInspectionTasks.setCurrentWaypointIndex(currentWaypointIndex);
            }
            if (status == 4) {
                feisuanInspectionTasks.setEndTime(LocalDateTime.now());
            }
            Integer res = inspectionTaskMapper.updateById(feisuanInspectionTasks);
        }

    }

    @Override
    public void editInspectionTaskEndTimeByFlightId(String flightId){
        QueryWrapper<FeisuanInspectionTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flight_id", flightId);
        FeisuanInspectionTask feisuanInspectionTasks = inspectionTaskMapper.selectOne(queryWrapper);
        if(feisuanInspectionTasks!=null){
            LocalDateTime now = LocalDateTime.now();
            feisuanInspectionTasks.setEndTime(now);
            Integer res = inspectionTaskMapper.updateById(feisuanInspectionTasks);
        }

    }
}
